{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c2261b9c",
   "metadata": {},
   "source": [
    "测试数据生成 - 正弦波数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9503a2a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.00000000e+00  8.66025404e-01  8.66025404e-01  1.22464680e-16\n",
      " -8.66025404e-01 -8.66025404e-01 -2.44929360e-16  8.66025404e-01\n",
      "  8.66025404e-01  3.67394040e-16]\n"
     ]
    }
   ],
   "source": [
    "# 导入依赖\n",
    "import numpy as np\n",
    "\n",
    "# 定义常数\n",
    "fs = 120e6  # 采样频率120MHz\n",
    "T = 1 / fs  # 采样周期\n",
    "f0 = 20e6   # 信号频率1MHz\n",
    "N = 1024    # 采样点数\n",
    "\n",
    "# 生成时间序列\n",
    "t = np.arange(N) * T # 时间向量\n",
    "# 生成正弦波信号\n",
    "signal = np.sin(2 * np.pi * f0 * t)\n",
    "# 输出前10个采样点\n",
    "print(signal[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bfc6cee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算并输出信号的频谱\n",
    "spectrum = np.fft.fft(signal)\n",
    "frequencies = np.fft.fftfreq(N, T)\n",
    "magnitude = np.abs(spectrum)\n",
    "print(magnitude[:10])\n",
    "#!/usr/bin/env python3\n",
    "# 导入依赖\n",
    "import numpy as np  \n",
    "import matplotlib.pyplot as plt \n",
    "\n",
    "# 定义常数\n",
    "fs = 120e6  # 采样频率120MHz\n",
    "T = 1 / fs  # 采样周期\n",
    "f0 = 20e6   # 信号频率20MHz\n",
    "N = 1024    # 采样点数\n",
    "# 生成时间序列\n",
    "\n",
    "t = np.arange(N) * T # 时间向量\n",
    "# 生成正弦波信号\n",
    "signal = np.sin(2 * np.pi * f0 * t)\n",
    "# 计算信号的频谱\n",
    "spectrum = np.fft.fft(signal)   \n",
    "frequencies = np.fft.fftfreq(N, T)\n",
    "magnitude = np.abs(spectrum)\n",
    "# 绘制时域信号\n",
    "plt.figure(figsize=(12, 6))\n",
    "plt.subplot(2, 1, 1)\n",
    "plt.plot(t[:100], signal[:100])  # 只绘制前100个\n",
    "plt.title('Time Domain Signal')\n",
    "plt.xlabel('Time (s)')\n",
    "plt.ylabel('Amplitude')\n",
    "# 绘制频域信号\n",
    "plt.subplot(2, 1, 2)\n",
    "plt.plot(frequencies[:N//2], magnitude[:N//2])  # 只\n",
    "plt.title('Frequency Domain Signal')\n",
    "plt.xlabel('Frequency (Hz)')\n",
    "plt.ylabel('Magnitude')\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
